package champion.virus;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.WindowManager;

public class AnnoyActivity extends Activity {
    MediaPlayer mySong;
    Vibrator myVibrator;

    // Start immediately
    // Vibrate for 200 milliseconds
    // Sleep for 500 milliseconds
    long[] vibePattern = { 0, 500, 200 };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// prevent app from attempting to change the screen orientation
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	setContentView(R.layout.annoy);

	mySong = MediaPlayer.create(AnnoyActivity.this, R.raw.kalimba);
	mySong.setLooping(true);

	// Get instance of Vibrator from current Context
	myVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

	// The "0" means to repeat the pattern starting at the beginning
	myVibrator.vibrate(vibePattern, 0);
    }

    @Override
    protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();

	// resume playing song
	mySong.start();

	// resume vibrating
	myVibrator.vibrate(vibePattern, 0);
    }

    @Override
    public void onPause() {
	super.onPause();

	// pause song at current position
	mySong.pause();
	myVibrator.cancel();
    }

    @Override
    protected void onStop() {
	// TODO Auto-generated method stub
	super.onStop();
	mySong.release();
    }

    // Disable all buttons except volume up and back
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//	if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//	    // preventing default implementation previous to
//	    // android.os.Build.VERSION_CODES.ECLAIR
//
//	    return super.onKeyDown(keyCode, event);
//	}
	if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
	    // preventing default implementation previous to
	    // android.os.Build.VERSION_CODES.ECLAIR

	    return super.onKeyDown(keyCode, event);
	}

	return true;
    }

    // Disable home button
    @Override
    public void onAttachedToWindow() {
	this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	super.onAttachedToWindow();
    }

}